local Enemy = class("Enemy");
local speed = 1;
local bulletSpeed = 10;
function Enemy:ctor(go)
	self.gameObject = go;
	self.transform = go.transform;
	self.firePoint = self.transform:Find("firePoint");
    self.hp = 10;
    self.lastFireTime = 0;
	
	
	local luaTrigger = self.gameObject:AddComponent(typeof(LuaTrigger2D));
	luaTrigger:SetExitFunc(self.OnTriggerExit, self);
	luaTrigger:SetEnterFunc(self.OnTriggerEnter, self);
	
	UpdateBeat:Add(self.Update, self);
end

function Enemy:Update()	
	self.transform:Translate(Vector3.down * speed * Time.deltaTime);
	self:Fire();						
end

function Enemy:Fire()
	if(Time.time - self.lastFireTime >0.8) then
		self.lastFireTime = Time.time;
		
		
			local go = poolMgr:Get("EnemyBullet");
			go.transform.position = self.firePoint.position;
			go.transform.rotation = self.firePoint.rotation;
			local body = go:GetComponent("Rigidbody2D");
			body.velocity = self.firePoint.up * bulletSpeed;
		
	end
	
end

function Enemy:OnTriggerExit(collision)
	if(collision:CompareTag("Destroy")) then
		self:Destroy();
	end
	
end
function Enemy:OnTriggerEnter(collision)
	if(collision:CompareTag("PlayerBullet")) then
		poolMgr:Release("PlayerBullet",collision.gameObject);
		self.hp = self.hp - 1;
		if(self.hp <= 0) then
			self:Destroy();
			Event.Brocast("EnemyDead");
		end	
	end
end
function Enemy:Destroy()
	UpdateBeat:Remove(self.Update, self);
	destroy(self.gameObject);
end
return Enemy; 